package com.kma.summer2012.client.presenters.authorization;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.events.ShowPreloaderEvent;
import com.kma.summer2012.client.events.authorization.AuthentificationEvent;
import com.kma.summer2012.client.events.authorization.PasswordRecoveryEvent;
import com.kma.summer2012.client.presenters.Presenter;
import com.kma.summer2012.client.services.AbstractCallBack;
import com.kma.summer2012.client.services.user.CrumbsUserService;
import com.kma.summer2012.shared.model.UICrumbsUser;

//import com.google.inject.Inject;

public class AuthorizationPresenter implements Presenter {

    public interface Display {
        HasClickHandlers getPassRecovery();

        HasClickHandlers getLoginButton();

        public HasKeyPressHandlers getKeyLoginButton();

        String getLoginTextBox();

        public HasKeyPressHandlers getKeyLoginBox();

        public HasKeyPressHandlers getKeyPasswordBox();

        String getPasswordTextBox();

        void setPasswordTextBox(String text);

        void setLoginTextBox(String text);

        CheckBox getShowPassword();

        void changePasswordTextBoxType();

        void setTextWrongPassword(String text);

        Widget asWidget();
    }

    private final Display display;
    private HasWidgets container;

    // @Inject
    public AuthorizationPresenter(Display display) {
        this.display = display;
        bind();
    }

    public void bind() {
        this.display.getLoginButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doLogin();
            }
        });
        this.display.getShowPassword().addValueChangeHandler(new ValueChangeHandler<Boolean>() {
            @Override
            public void onValueChange(ValueChangeEvent<Boolean> event) {
                AuthorizationPresenter.this.display.changePasswordTextBoxType();
            }
        });
        this.display.getPassRecovery().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                CrumbsEventBus.getInstance().fireEvent(new PasswordRecoveryEvent());
            }
        });
        this.display.getKeyLoginButton().addKeyPressHandler(new KeyPressHandler() {
            @Override
            public void onKeyPress(KeyPressEvent keyPressEvent) {
                boolean enterPressed = KeyCodes.KEY_ENTER == keyPressEvent.getNativeEvent().getKeyCode();
                if (enterPressed) {
                    doLogin();
                }
            }
        });
        this.display.getKeyLoginBox().addKeyPressHandler(new KeyPressHandler() {
            @Override
            public void onKeyPress(KeyPressEvent keyPressEvent) {
                boolean enterPressed = KeyCodes.KEY_ENTER == keyPressEvent.getNativeEvent().getKeyCode();
                if (enterPressed) {
                    doLogin();
                }
            }
        });
        this.display.getKeyPasswordBox().addKeyPressHandler(new KeyPressHandler() {
            @Override
            public void onKeyPress(KeyPressEvent keyPressEvent) {
                boolean enterPressed = KeyCodes.KEY_ENTER == keyPressEvent.getNativeEvent().getKeyCode();
                if (enterPressed) {
                    doLogin();
                }
            }
        });

    }

    @Override
    public void go(HasWidgets container) {
        this.container = container;
        container.clear();
        container.add(display.asWidget());
    }

    public void doLogin() {
        CrumbsEventBus.getInstance().fireEvent(new ShowPreloaderEvent());

        CrumbsUserService.App.getInstance().getByLoginPassword(
                AuthorizationPresenter.this.display.getLoginTextBox(),   //TODO When I read method name I think what you send to service widgets not value
                AuthorizationPresenter.this.display.getPasswordTextBox(),
                new AbstractCallBack<UICrumbsUser>() {
                    public void onSuccess(UICrumbsUser result) {
                        if (result != null) {
                            AuthorizationPresenter.this.display.setLoginTextBox("");
                            AuthorizationPresenter.this.display.setPasswordTextBox("");
                            // AuthorizationPresenter.this.display.setTextWrongPassword("login success, hello "+result.getSurnmNmSecondnm());
                            CrumbsEventBus.getInstance().fireEvent(new AuthentificationEvent(result));
                        } else {
                            AuthorizationPresenter.this.container.clear();
                            AuthorizationPresenter.this.go(RootPanel.get("content"));
                            AuthorizationPresenter.this.display.setTextWrongPassword("Invalid password or login!");
                        }
                    }
                });
    }
}
